#include <cassert>
#include <cstring>

#include <GA\BinaryString\BoolArray.hpp>

unitraf::BoolArray::BoolArray( size_t size )
{
	//-----------------debug section----------
	assert ( size > 0 );
	//--------------end of debug section------

	_length = size;
	_array = new bool [ _length ];
}


unitraf::BoolArray::~BoolArray( )
{
	delete _array;
	_array = 0;
}

size_t unitraf::BoolArray::getLength( ) const
{
	return _length;
}

bool unitraf::BoolArray::getBit( size_t pos ) const
{
	return _array[ pos ];
}

void unitraf::BoolArray::setBit( size_t pos, bool bit/* = true*/ )
{
	_array[ pos ] = bit;
}

void unitraf::BoolArray::flipBit( size_t pos )
{
	_array[ pos ] = !_array[ pos ];
}

unitraf::BinaryString* unitraf::BoolArray::clone( ) const
{
	BoolArray* clone = new BoolArray( _length );
	
	//clone->_length = _length;
	memcpy( clone->_array, _array, _length * sizeof(bool) );

	return clone;
}


unsigned long unitraf::BoolArray::toUlong( ) const
{
	return toUlong(0, _length );
}

unsigned long unitraf::BoolArray::toUlong(size_t begin, size_t end ) const
{
	//--------------debug----------
	assert( begin >= 0 );
	assert( end <= _length );
	assert( begin < end );
	//------------end of debug-----

	unsigned long res = 0;

	for ( size_t i = begin; i < end; ++i )
	{
		res = res << 1;
		res += static_cast<unsigned long> (_array[i]);
	}

	return res;
}

